package com.ruoyi.ly.utils;



import com.alibaba.excel.EasyExcel;
import com.ruoyi.ly.domain.SpaceBan;
import com.ruoyi.ly.domain.vo.PageSpaceBanVO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//导出数据工具类
@Component
public class ExportData {
    public void exportDataEasyExcel(HttpServletResponse response, List<?> list, Class<?> c, String name) {
        try {
            //设置响应头信息和其他信息
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");

            //获取当前时间
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
            String formattedDate = currentDate.format(formatter);

            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = null;
            fileName = URLEncoder.encode(name +"---" + formattedDate, "UTF-8");

            //设置响应头信息 Content-disposition
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //调用EasyExcel的write   list是我们需要导出的数据列表  c是我们导出数据的实体类
            EasyExcel.write(response.getOutputStream(), c)
                    .sheet(name).doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

